/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import path from 'path';

import { expect } from '@jest/globals';
import fse from 'fs-extra';

import { runHvigorAsync } from '../../common/common';
import { commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';


const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [
    ModuleTemplateType.StaticLibrary
  ]
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();

  const module = project.getModule('staticLibrary');
  const moduleRootPath = module.moduleRootPath();
  // eslint-disable-next-line max-len
  const commands = '--mode module -p product=default -p module=staticLibrary@default -p buildMode=release assembleHar --analyze=normal --parallel --incremental --daemon'.split(' ');
  const artifactRoot = path.resolve(moduleRootPath, 'build/default/intermediates/loader/default/pkgContextInfo.json');

  await commonJson5Operation(path.resolve(projectRootPath, 'build-profile.json5'), json => {
    json.app.products[0].buildOption = {};
    json.app.products[0].buildOption.strictMode = {'useNormalizedOHMUrl' : true};
    return json;
  });

  const result1 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('首次构建，GeneratePkgContextInfo为Finished', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
  expect(result1.stdout).toContain('Finished :staticLibrary:default@GeneratePkgContextInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  const result2 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('不做任何更改，再次构建，GeneratePkgContextInfo为UP-TO-DATE', result2);
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  expect(result2.stdout).toContain('UP-TO-DATE :staticLibrary:default@GeneratePkgContextInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  // 修改outputFiles
  fse.removeSync(artifactRoot);
  const result3 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('删除产物，再次构建，GenerateMetadata为Finished', result3);
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
  expect(result3.stdout).toContain('Finished :staticLibrary:default@GeneratePkgContextInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  // 修改inputs pkg内容
  await commonJson5Operation(path.resolve(moduleRootPath, 'oh-package.json5'), json => {
    json.version = '2.0.0';
    return json;
  });
  const result4 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('修改pkgMap文件，再次构建，GeneratePkgContextInfo为Finished', result4);
  expect(result4.stdout).toContain('BUILD SUCCESSFUL');
  expect(result4.stdout).toContain('Finished :staticLibrary:default@GeneratePkgContextInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

};

TestCaseBuilder.builder()
  .caseName('GeneratePkgContextInfo')
  .description('GeneratePkgContextInfo任务增量测试用例')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L2).build())
  .testFun(testfun)
  .load();
